<template>

    <el-popover
            placement="bottom"
            width="260"
            trigger="click">
        <el-button size="small" class="select" type="warning" @click="exportCurrentPage">导出当前页</el-button>
        <el-button size="small" class="select" type="warning" @click="exportAllPage">导出所有页</el-button>
        <el-button size="small" slot="reference" class="select" icon="el-icon-download" type="warning">导出</el-button>
    </el-popover>

</template>

<script>
    import {exportTableData} from "./request";
    import {timestamp} from "ali-oss";

    export default {
        name: 'SJExport',
        props: ['config'],
        data() {
            return {
                current: 1,
                // privateConfig: {
                //     url: '/deviceManager/exportExcel',
                //     method: 'POST',
                //     mineId: false,
                //     currentPage: 1,
                //     pageSize: 10,
                //     filename: '',
                //     data: {}
                // }
                privateConfig: this.config
            }
        },
        methods: {
            exportCurrentPage() {

                
                if(!this.$routeParam()) {
                    this.privateConfig.currentPage = 1
                }else{
                    this.privateConfig.currentPage = this.$currentPage('page')
                }
                this.request()
            },

            exportAllPage() {
                this.privateConfig.currentPage = 1
                // 设置最多下载条数
                this.privateConfig.pageSize = 100000
                this.request()
            },
            request() {
                let requestJson = this.privateConfig
                if(this.$routeParam()) {
                    requestJson.data = this.$routeParam()
                }
                if(requestJson.mineId) {
                    requestJson.data.mineId = this.$store.state.mineId
                }
                if(requestJson.companyId) {
                    requestJson.data.companyId = this.$store.state.companyId
                }

                exportTableData(requestJson).then(response=>{
                    console.log(response);
                    const blob = new Blob([response]);
                    const elink = document.createElement('a');
                    var fileName = ''
                  
                    if(this.privateConfig.filename) {
                        let timestamp = new Date().getTime()
                        fileName = this.privateConfig.filename + "_" + this.timestamp() + ".xls"
                    } else {
                        fileName = '导出数据.xls';
                    }
                    elink.download = fileName;
                    elink.style.display = 'none';
                    elink.href = URL.createObjectURL(blob);
                    document.body.appendChild(elink);
                    elink.click();
                    URL.revokeObjectURL(elink.href); // 释放URL 对象
                    document.body.removeChild(elink);
                }).catch(error => {
                    console.log(error)
                })

            },

            timestamp() {
                let yy = new Date().getFullYear();
                let mm = new Date().getMonth()+1;
                let dd = new Date().getDate();
                let hh = new Date().getHours();
                let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
                let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
                return ''+yy+mm+dd+hh+mf+ss
            }
        },

        watch: {
        },
        created() {
        }
    }
</script>

<style>
</style>